package com.jxc.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jxc.dao.BaseDaoI;
import com.jxc.model.po.Goods;
import com.jxc.model.po.GoodsRepertory;
import com.jxc.model.po.Repertory;
import com.jxc.model.vo.VGoodsRepertory;
import com.jxc.service.GoodsRepertoryServiceI;

@Service("goodsRepertoryService")
public class GoodsRepertoryServiceImpl implements GoodsRepertoryServiceI {
	

	@Autowired
	private BaseDaoI<GoodsRepertory> goodsreperDao;

	@Autowired
	private BaseDaoI<Goods> goodsDao;
	
	/* (non-Javadoc)
	 * @see com.jxc.service.impl.GoodsRepertoryI#updateOut(com.jxc.model.vo.VGoodsRepertory)
	 */
	public void updateOut(VGoodsRepertory vgr){
		GoodsRepertory gr = goodsreperDao.get(GoodsRepertory.class, vgr.getId());
		int nu = gr.getNum()-vgr.getNum();
		BeanUtils.copyProperties(vgr, gr);
		if(vgr.getGid()!=null){
			gr.setGoods(new Goods(vgr.getGid()));
		}if(vgr.getOrid()!=null){
			gr.setRepertory(new Repertory(vgr.getOrid()));
		}if(vgr.getNum()!=null){
			gr.setNum(nu);
		}
		goodsreperDao.update(gr);
	}
	
	public void findOut(VGoodsRepertory vg){
		String sql2 = "select t from GoodsRepertory t where t.goods.id="+vg.getGid()+" and t.repertory.id="+vg.getOrid();
		List<GoodsRepertory> gr2 = goodsreperDao.find(sql2);
		GoodsRepertory gr = gr2.get(0);
		vg.setId(gr.getId());
		updateOut(vg);
	}
	/* (non-Javadoc)
	 * @see com.jxc.service.impl.GoodsRepertoryI#updateIn(com.jxc.model.vo.VGoodsRepertory)
	 */
	public void updateIn(VGoodsRepertory vgr){
		GoodsRepertory gr = goodsreperDao.get(GoodsRepertory.class, vgr.getId());
		BeanUtils.copyProperties(vgr, gr);
		if(vgr.getGid()!=null){
			gr.setGoods(new Goods(vgr.getGid()));
		}if(vgr.getIrid()!=null){
			gr.setRepertory(new Repertory(vgr.getIrid()));
		}if(vgr.getNum()!=null){
			gr.setNum(vgr.getNum());
		}
		goodsreperDao.update(gr);
	}
	
	/* (non-Javadoc)
	 * @see com.jxc.service.impl.GoodsRepertoryI#findCounts(com.jxc.model.vo.VGoodsRepertory)
	 */
	public void updateCheck(VGoodsRepertory vgr){
		String sql2 = "select t from GoodsRepertory t where t.goods.id="+vgr.getGid()+" and t.repertory.id="+vgr.getIrid();
		List<GoodsRepertory> gr2 = goodsreperDao.find(sql2);
		if(gr2.size()>0){
			for(GoodsRepertory ff : gr2){
				VGoodsRepertory cc = new VGoodsRepertory();
				cc.setGid(ff.getGoods().getId());
				cc.setIrid(ff.getRepertory().getId());
				cc.setNum(vgr.getNum());
				cc.setId(ff.getId());
				this.updateIn(cc);
			}
		}
	}
	
	public void findCounts(VGoodsRepertory vgr){
		
		//清除GoodsRepertory 的缓存
		goodsreperDao.getSession().evict(GoodsRepertory.class);
		goodsreperDao.getSession().evict(Repertory.class);
		goodsreperDao.getSession().evict(Goods.class);
		
		
		String sql2 = "select t from GoodsRepertory t where t.goods.id="+vgr.getGid()+" and t.repertory.id="+vgr.getIrid();
		
		
		List<GoodsRepertory> gr2 = goodsreperDao.find(sql2);
		System.err.println(gr2.size());
		if(gr2.size()==0){
			Goods goods = goodsDao.get(Goods.class, new Long(vgr.getGid()));
			GoodsRepertory cr = new GoodsRepertory();
			cr.setGoods(goods);
			cr.setRepertory(new Repertory(vgr.getIrid()));
			cr.setNum(vgr.getNum());       
			goodsreperDao.save(cr);
			System.out.println(cr.getId());
		}else {
			int index=0;
			List<Object[]> objects=changeModel(vgr);
			
			for(GoodsRepertory ff : gr2){
				VGoodsRepertory cc = new VGoodsRepertory();
				cc.setGid(ff.getGoods().getId());
				cc.setIrid(ff.getRepertory().getId());
				
				Object[] os=objects.get(index);
				//当前库存数量+退货的数量
				
				//cc.setNum(Integer.parseInt(os[1].toString())+vgr.getNum());
				cc.setNum(ff.getNum()+vgr.getNum());
//				GoodsRepertory cr = goodsreperDao.get(GoodsRepertory.class, ff.getId());
//				cr.setNum(cr.getNum()+vgr.getNum());
				cc.setId(ff.getId());
				this.updateIn(cc);
				index++;
			}
		}
	}
	
	
	private List<Object[]> changeModel(VGoodsRepertory vgr){
		
		
		String sql="select gr.* from spgl_goods g,rkgl_repertory r,RKGL_GOODS_REPERTORY gr" +
					" where gr.goods_id=g.ID and r.ID=gr.REPERTORY_ID" +
					"	and gr.goods_id=" +vgr.getGid()+
					" 	and gr.REPERTORY_ID="+vgr.getIrid();
		
		
		return goodsreperDao.findBySqlClean(sql);
	}
	
	
	public Integer findNums(Long gid,Long rid){
		Integer i = 0;
		try {
			String hql = "select t.num from GoodsRepertory t where t.goods="+gid+" and t.repertory="+rid;
			i = goodsreperDao.count2(hql);
			if(i==null)i=0;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return i;
	}
}
